<template>
	<view class="login-wrap">
		<image src="../../static/logo.png" mode=""></image>
		<view class="tips">
			<view>申请获取以下权限，请确认是本人操作</view>
			<text>· 获取你的公开信息（昵称、头像等）</text>
		</view>
		<button class="submit-btn" open-type="getUserInfo" @getUserInfo="getUserInfo">登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: null
			}
		},
				
		onLoad() {
			uni.login({
				success(res) {
					if (res.errMsg == 'login:ok') {
						this.code == res.code
					} else {
						uni.showToast({
							title: '系统异常，请联系管理员'
						})
					}
				}
			})
		},
				
		methods: {
			getUserInfo(e) {
				let that = this
				let setting = this.getSetting()
				setting.then(res => {
					console.log('setting is Auth', res)
					if (res) {
						//eData  包括//微信头像//微信名称 还有加密的数据.
						let eData = JSON.parse(e.detail.rawData);
						that.$store.dispatch('common/login', {
							eData,
							code: that.code
						}).then(() => {
							uni.showToast({
								title: '系统异常，请联系管理员'
							})
						})
					} else {
						uni.showToast({
							title: '授权失败，请确认授权已开启',
							mask: true,
							icon: 'none'
						})
					}
				})
			},
			
			getSetting() {
				return new Promise(function(resolve, reject) {
					uni.getSetting({
						success: function(res) {
							if (res.authSetting['scope.userInfo']) {
								console.log('存在');
								resolve(true);
							} else {
								console.log('不存在');
								resolve(false);
							}
						}
					})
				}).catch((e) => {
					console.log(e)
				});;
			},
		}
	}
</script>

<style lang="scss">
.login-wrap {
	width: 80%;
	margin: 20rpx auto;
	text-align: center;
	
	image {
		width: 200rpx;
		height: 200rpx;
		margin: 40rpx auto 60rpx;
	}
	
	.tips {
		margin: 0 auto 80rpx;
		padding-top: 50rpx;
		font-size: 34rpx;
		color: #333333;
		border-top: 1px solid #4f4f4f;
		text-align: left;
		line-height: 2;
		
		text {
			display: block;
			font-size: 28rpx;
			color: #666666;
		}
	}
}
</style>
